﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WifiCan.component
{
    internal class CanListView
    {
        // 传输方向
        public enum CAN_DIRECTION_TYPE
        {
            CAN_DIRECTION_TYPE_UNKNOW,      // 未知
            CAN_DIRECTION_TYPE_SEND,        // 发送
            CAN_DIRECTION_TYPE_RECV,        // 接收
        }

        // 传输格式
        public enum CAN_DATA_FORMAT
        {
            CAN_DATA_FORMAT_UNKNOW,         // 未知
            CAN_DATA_FORMAT_DATA_FRAME,     // 数据帧
            CAN_DATA_FORMAT_REMOTE_FRAME,   // 远程帧
        }

        // 传输格式
        public enum CAN_DATA_TYPE
        {
            CAN_DATA_TYPE_UNKNOW,           // 未知
            CAN_DATA_TYPE_STD,              // 标准帧
            CAN_DATA_TYPE_EXT,              // 扩展帧
        }

        // 传输消息
        public class CANMessage
        {
            public CAN_DIRECTION_TYPE direction;           // 传输方向
            public UInt32 time;                            // 时间戳
            public UInt32 id;                              // CAN ID
            public CAN_DATA_FORMAT format;                 // 帧格式
            public CAN_DATA_TYPE type;                     // 传输格式
            public int size;                               // 数据长度
            public byte[]? data;                           // 数据

            public CANMessage()
            {
                direction = CAN_DIRECTION_TYPE.CAN_DIRECTION_TYPE_UNKNOW;
                time = 0;
                id = 0;
                format = CAN_DATA_FORMAT.CAN_DATA_FORMAT_UNKNOW;
                type = CAN_DATA_TYPE.CAN_DATA_TYPE_UNKNOW;
                size = 0;
                data = null;
            }
        }

        private ListView target;
        private int count = 0;
        private bool isAutoRoll = false;

        public CanListView(ListView target) {
            this.target = target;
            this.target.GridLines = true;//表格是否显示网格线
            this.target.FullRowSelect = true;//是否选中整行

            this.target.View = View.Details;//设置显示方式
            this.target.Scrollable = true;//是否自动显示滚动条
            this.target.MultiSelect = false;//是否可以选择多行

            //添加表头（列）
            this.target.Columns.Add("number", "序号");
            this.target.Columns.Add("direction", "传输方向");
            this.target.Columns.Add("time", "时间标识");
            this.target.Columns.Add("id", "帧ID");
            this.target.Columns.Add("format", "帧格式");
            this.target.Columns.Add("type", "帧类型");
            this.target.Columns.Add("size", "数据长度");
            this.target.Columns.Add("data", "数据(HEX)");

            this.target.Columns["number"].Width = -2;//根据表头设置宽度
            this.target.Columns["direction"].Width = -2;//根据表头设置宽度
            this.target.Columns["time"].Width = -2;//根据表头设置宽度
            this.target.Columns["id"].Width = -2;//根据表头设置宽度
            this.target.Columns["format"].Width = -2;//根据表头设置宽度
            this.target.Columns["type"].Width = -2;//根据表头设置宽度
            this.target.Columns["size"].Width = -2;//根据表头设置宽度
            this.target.Columns["data"].Width = -2;//根据表头设置宽度

            CANMessage message = new CANMessage();
            message.direction = CAN_DIRECTION_TYPE.CAN_DIRECTION_TYPE_SEND;
            message.time = 0;
            message.id = 0;
            message.format = CAN_DATA_FORMAT.CAN_DATA_FORMAT_DATA_FRAME;
            message.type = CAN_DATA_TYPE.CAN_DATA_TYPE_STD;
            message.size = 8;
            message.data = [1, 2, 3, 4, 5, 6, 7, 8];

            for (int i = 0; i < 10; i++)
            {
                addItemToView(message);
            }

        }

        /// <summary>
        /// 添加条目
        /// </summary>
        /// <param name="message"></param>
        public void addItemToView(CANMessage message)
        {
            ListViewItem item = new ListViewItem();

            string _number = "";
            string _direction = "";
            string _time = "";
            string _id = "";
            string _format = "";
            string _type = "";
            string _size = "";
            string _data = "";

            _number = this.count.ToString();
            _time = message.time.ToString();
            _id = $"{message.id:X}";
            _size = message.size.ToString();
            this.count += 1;

            if (message.direction == CAN_DIRECTION_TYPE.CAN_DIRECTION_TYPE_SEND)
            {
                _direction = "发送";
            }
            else if (message.direction == CAN_DIRECTION_TYPE.CAN_DIRECTION_TYPE_RECV)
            {
                _direction = "接收";
            }
            else
            {
                _direction = "未知";
            }

            if (message.format == CAN_DATA_FORMAT.CAN_DATA_FORMAT_DATA_FRAME)
            {
                _format = "数据帧";
            }
            else if (message.format == CAN_DATA_FORMAT.CAN_DATA_FORMAT_REMOTE_FRAME)
            {
                _format = "远程帧";
            }
            else
            {
                _format = "未知";
            }

            if (message.type == CAN_DATA_TYPE.CAN_DATA_TYPE_STD)
            {
                _type = "标准帧";
            }
            else if (message.type == CAN_DATA_TYPE.CAN_DATA_TYPE_EXT)
            {
                _type = "扩展帧";
            }
            else
            {
                _type = "未知";
            }

            if (message.size != 0 && message.data != null)
            {
                for (int i = 0; i < message.size; i++)
                {
                    _data = _data + $"{message.data[i]:X2} ";
                }
            }

            item.SubItems.Clear();
            item.SubItems[0].Text = _number;
            item.SubItems.Add(_direction);
            item.SubItems.Add(_time);
            item.SubItems.Add(_id);
            item.SubItems.Add(_format);
            item.SubItems.Add(_type);
            item.SubItems.Add(_size);
            item.SubItems.Add(_data);

            this.target.Items.Add(item);

            if(isAutoRoll)
            {
                this.target.Items[this.target.Items.Count - 1].EnsureVisible();
            }
        }

        public void clearAllItem()
        {
            this.target.Clear();

            //添加表头（列）
            this.target.Columns.Add("number", "序号");
            this.target.Columns.Add("direction", "传输方向");
            this.target.Columns.Add("time", "时间标识");
            this.target.Columns.Add("id", "帧ID");
            this.target.Columns.Add("format", "帧格式");
            this.target.Columns.Add("type", "帧类型");
            this.target.Columns.Add("size", "数据长度");
            this.target.Columns.Add("data", "数据(HEX)");

            this.target.Columns["number"].Width = -2;//根据表头设置宽度
            this.target.Columns["direction"].Width = -2;//根据表头设置宽度
            this.target.Columns["time"].Width = -2;//根据表头设置宽度
            this.target.Columns["id"].Width = -2;//根据表头设置宽度
            this.target.Columns["format"].Width = -2;//根据表头设置宽度
            this.target.Columns["type"].Width = -2;//根据表头设置宽度
            this.target.Columns["size"].Width = -2;//根据表头设置宽度
            this.target.Columns["data"].Width = -2;//根据表头设置宽度
        }

        /// <summary>
        /// 设置自动滚动模式
        /// </summary>
        /// <param name="mode">true开启自动滚动</param>
        public void autoRollModeSet(bool mode)
        {
            isAutoRoll = mode;
        }
    }
}
